{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# RPA Challenge Solution\n",
    "\n",
    "This example illustrates a solution to the well known 'rpa challenge' on www.rpachallenge.com. The task at hand consists of downloading a file and filling in a changing webform. With a decent internet connection the Automagica bot should be able to complete this in under 5 seconds."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "from automagica import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Open browser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "browser = Chrome()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "browser.get('http://www.rpachallenge.com/')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Read Excel data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "challenge_excel = download_file_from_url('http://www.rpachallenge.com/assets/downloadFiles/challenge.xlsx')\n",
    "\n",
    "excel = Excel(file_path=challenge_excel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = excel.read_worksheet('Sheet1', headers=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Start the challenge\n",
    "\n",
    "One of the challenges is to find the names (technical identifiers) of the buttons for the different webforms. Check out the browser automation for a more in-depth tutorial on how to find these elements or use the Automagica Wand to use vision in finding these elements."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "browser.find_element_by_class_name('btn-large').click()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Fill in the forms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "for row in data:\n",
    "    \n",
    "    inputs = browser.find_elements_by_tag_name('input')\n",
    "    \n",
    "    for input_ in inputs:\n",
    "        name = input_.get_attribute('ng-reflect-name')\n",
    "        \n",
    "        if name == 'labelFirstName':\n",
    "            input_.send_keys(row['First Name'])\n",
    "            \n",
    "        if name == 'labelPhone':\n",
    "            input_.send_keys(int(row['Phone Number']))\n",
    "            \n",
    "        if name == 'labelCompanyName':\n",
    "            input_.send_keys(row['Company Name'])\n",
    "            \n",
    "        if name == 'labelEmail':\n",
    "            input_.send_keys(row['Email'])\n",
    "            \n",
    "        if name == 'labelAddress':\n",
    "            input_.send_keys(row['Address'])\n",
    "            \n",
    "        if name == 'labelLastName':\n",
    "            input_.send_keys(row['Last Name '])\n",
    "            \n",
    "        if name == 'labelRole':\n",
    "            input_.send_keys(row['Role in Company'])\n",
    "            \n",
    "        if input_.get_attribute('type') == 'submit':\n",
    "            input_.click()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Extract final score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Your success rate is 100% ( 70 out of 70 fields) in 4770 milliseconds'"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "browser.find_element_by_class_name('message2').text"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Close browser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "browser.quit()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
